import geopandas
from shapely import geometry
from matplotlib import pyplot as plt
import ast
import sys
import os
class CreateAreaImg:
    def __init__(self,params):
        # 面坐标
        self.coorPolygonList = params["coorPolygonList"]
        self.color = params["color"]
        self.imgPath = params["imgPath"]
        self.coorPolygonTuple = self.coorListToTuple()
        self.createFolder(self.imgPath)
    def createFolder(self,path):
        if not os.path.exists(os.path.dirname(path)):
            os.mkdir(os.path.dirname(path))
    def coorListToTuple(self):
        return [tuple(sublist) for sublist in self.coorPolygonList]
    def run(self):
        f,ax = plt.subplots(dpi=80)
        # p = [(118.7375944222993,39.7039150480989), (118.7416280060988,39.70395874841385), (118.75317814985986,39.70033666987302), (118.76134505887829,39.700027903445736), (118.75882642456268,39.68442877931079), (118.7375944222993,39.7039150480989)]
        polygon = geopandas.GeoSeries([geometry.Polygon(self.coorPolygonTuple)], crs='EPSG:4326', index=['1001'])
        # polygon = geopandas.GeoSeries([geometry.Polygon([(40391550.33,4397065.78),(40391577.33,4397015.78),(40391573.33,4396861.78), (40391345.33,4396861.77),(40391345.33,4397099.77),(40391550.33,4397065.78)])], crs='EPSG:4528', index=['1001'])

        plt.axis('off')
        plt.margins(0, 0)
        fig = plt.gcf()
        fig.set_alpha(0)
        polygon.plot(ax=ax, alpha=0.8,color=self.color)

        f.savefig(self.imgPath,bbox_inches='tight', pad_inches=0,transparent=True)

coorPolygonStr = sys.argv[1]
coorPolygonList = ast.literal_eval(coorPolygonStr)
color = sys.argv[2]
imgPath = sys.argv[3]
params = {
        "coorPolygonList":coorPolygonList,
        "color":color,
        "imgPath":imgPath,
}

try:
    # CreateAreaImg(params)
    CreateAreaImg(params).run()
    print("true")
except Exception as ex:
    print(ex)
    print("false")
